<template>
  <el-image
    :src="realSrc()"
    fit="cover"
    :style="`width:${realWidth};height:${realHeight};`"
    :preview-src-list="realSrcList()"
  >
    <div slot="error" class="image-slot">
      <i class="el-icon-picture-outline"></i>
    </div>
  </el-image>
</template>

<script>
import { isExternal } from "@/utils/validate";

// 图片预览查看组件
export default {
  name: "ImagePreview",
  props: {
    src: {
      type: String,
      required: true
    },
    width: {
      type: [Number, String],
      default: ""
    },
    height: {
      type: [Number, String],
      default: ""
    }
  },
  computed: {
    realWidth() {
      return typeof this.width == "string" ? this.width : `${this.width}px`;
    },
    realHeight() {
      return typeof this.height == "string" ? this.height : `${this.height}px`;
    }
  },
  methods: {
    // 单张图片
    realSrc() {
      // console.log(222,this.src);
      let real_src = this.src ? this.src.split(",")[0] : this.src;
      if (isExternal(real_src) || real_src.indexOf(process.env.VUE_APP_BASE_API) > -1 || real_src.indexOf('http') > -1) {
        // 带了http
        return real_src;
      }
      // console.log(333,process.env.VUE_APP_BASE_API + real_src)
      return process.env.VUE_APP_BASE_API + real_src;
    },

    // 多张图片 逗号分割
    realSrcList() {
      // console.log(this.src);
      let real_src_list = this.src ? this.src.split(",") : [];
      // console.log(real_src_list);
      let srcList = [];

      real_src_list.forEach(item => {
        if (isExternal(item) || item.indexOf(process.env.VUE_APP_BASE_API) > -1) {
           srcList.push(item);
        }else{
          srcList.push(process.env.VUE_APP_BASE_API + item);
        }

      });
      // console.log(srcList);
      return srcList;
    },

  },
};
</script>

<style lang="scss" scoped>
.el-image {
  border-radius: 5px;
  background-color: #ebeef5;
  box-shadow: 0 0 5px 1px #ccc;
  ::v-deep .el-image__inner {
    transition: all 0.3s;
    cursor: pointer;
    &:hover {
      transform: scale(1.2);
    }
  }
  ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    color: #909399;
    font-size: 30px;
  }
}
</style>
